<?php

namespace App\Models\Customers;

use App\Models\Address\AddressModel;
use App\Models\BasicModel;

class CustomersModel extends BasicModel
{
    protected $table = 'customers';

    protected $primaryKey = 'customer_id';

    protected $fillable = [
        'customer_id',
        'com_id',
        'avatar_id',
        'wx_union_id',
        'wx_open_id',
        'name',
        'password',
        'email',
        'phone',
        'union_id',
        'enable_status',
        'status',
        'pu_key',
        'pv_key',
        'staff_last_login_time',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'customer_id' => 'integer',
        'com_id' => 'integer',
        'avatar_id' => 'string',
        'wx_union_id' => 'string',
        'wx_open_id' => 'string',
        'name' => 'string',
        'password' => 'string',
        'email' => 'string',
        'phone' => 'string',
        'union_id' => 'string',
        'enable_status' => 'integer',
        'status' => 'integer',
        'pu_key' => 'string',
        'pv_key' => 'string',
        'staff_last_login_time' => 'string', // 这里给string 返回 2023-11-23 17:13:20 否则  "2023-11-23T09:04:40.000000Z";
        'created_at' => 'string',
        'updated_at' => 'string',
    ];

    protected $hidden = [
        'wx_union_id',
        'wx_open_id',
        'union_id',
        'pu_key',
        'pv_key',
        'password',
        'enable_status',
        'status',
        'created_at',
        'updated_at',
    ];

    /**
     * 根据用户账号获取用户信息
     * @param string $account
     * @return mixed
     */
    public function getUsersInfoByAccount(string $account): mixed
    {
        return $this
            ->where('account',$account)
            ->where('status',1)
            ->where('enable_status',1)
            ->first();
    }

    /**
     * 获取顾客的地址
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function address()
    {
        return $this->belongsTo(AddressModel::class,'customer_id','customer_id');
    }

}
